package cn.cqone.crm.service.impl;

import cn.cqone.crm.domain.Employee;
import cn.cqone.crm.domain.Tenant;
import cn.cqone.crm.mapper.EmployeeMapper;
import cn.cqone.crm.mapper.SetMealMapper;
import cn.cqone.crm.mapper.TenantMapper;
import cn.cqone.crm.query.TenantQuery;
import cn.cqone.crm.service.TenantService;
import cn.cqone.crm.twoObject.TwoSimple;
import cn.cqone.crm.utils.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import shiro.MD5Utils;

import java.util.List;

@Service
@Transactional
public class TenantServiceImpl extends BaseServiceImpl<Tenant,Long, TenantQuery> implements TenantService {
    @Autowired
    private TenantMapper tenantMapper;
    @Autowired
    private SetMealMapper setMealMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Override
    public int settleIn(TwoSimple twoSimple) {
        Tenant tenant = twoSimple.getTenant();
        Employee employee = twoSimple.getEmployee();
        if (employeeMapper.findByName(employee.getUsername())==null){
            employee.setType(2);
            Long last = tenantMapper.findLast();
            last = last+1;
            tenant.setId(last);
            employee.setTenant(tenant);
            employee.setPassword(MD5Utils.getMD5Pawword(employee.getPassword()));
        }
        employeeMapper.add(employee);
        String logoUrl = tenant.getLogoUrl();
        tenant.setLogoUrl("http://localhost:8060/"+logoUrl);
        return super.add(tenant);
    }

    @Override
    public int update(Tenant obj) {
        obj.setLogoUrl("http://localhost:8060/"+obj.getLogoUrl());
        return super.update(obj);
    }

    @Override
    public PageList<Tenant> findByPage(TenantQuery query) {
        List<Tenant> list = tenantMapper.findList(query);
        return super.findByPage(query);
    }
}
